﻿using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;

namespace PasteLoggerBoard.serilogmodels
{
    /// <summary>
    ///
    ///</summary>
    [TypeFilter(typeof(RoleAttribute), Arguments = new object[] { "root", "root" })]
    public class AppInfoAppService : IUserAppService
    {

        private readonly IPasteLoggerBoardDbContext _dbContext;

        /// <summary>
        ///
        ///</summary>
        ///<param name="dbContext"></param>
        public AppInfoAppService(IPasteLoggerBoardDbContext dbContext) : base()
        {
            _dbContext = dbContext;
        }

        /// <summary>
        /// 按页获取
        ///</summary>
        ///<param name="input">页大小</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PagedResultDto<AppInfoListDto>> GetListAsync(InputSearchBase input)
        {

            var query = _dbContext.AppInfo.Where(t => 1 == 1).OrderByDescending(xy => xy.Id);
            var _pagedto = new PagedResultDto<AppInfoListDto>();
            if (input.page == 1)
            {
                _pagedto.TotalCount = await query.CountAsync();
            }
            var userList = await query.Page(input.page, input.size).ToListAsync();
            var temList = ObjectMapper.Map<List<AppInfo>, List<AppInfoListDto>>(userList);
            _pagedto.Items = temList;
            return _pagedto;
        }

        /// <summary>
        /// 按页获取
        ///</summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<AppInfoListDto>> Options()
        {

            var query = await _dbContext.AppInfo.Where(t => true).OrderBy(x => x.Sort).AsNoTracking().ToListAsync();
            var temList = ObjectMapper.Map<List<AppInfo>, List<AppInfoListDto>>(query);
            return temList;
        }

        /// <summary>
        /// 根据ID获取单项
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public AppInfoDto GetByIdAsync(int id)
        {
            var query = _dbContext.AppInfo.Where(t => t.Id == id).AsNoTracking().FirstOrDefault();
            if (query == null || query == default)
            {
                throw new PasteLoggerBoardException("没有找到这样的信息", 404);
            }
            var temList = ObjectMapper.Map<AppInfo, AppInfoDto>(query);
            return temList;
        }

        /// <summary>
        /// 根据ID获取待更新单项信息
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public AppInfoUpdateDto GetInfoForUpdateAsync(int id)
        {
            var query = _dbContext.AppInfo.Where(t => t.Id == id).AsNoTracking().FirstOrDefault();
            if (query == null || query == default)
            {
                throw new PasteLoggerBoardException("没有找到这样的信息", 404);
            }
            var temList = ObjectMapper.Map<AppInfo, AppInfoUpdateDto>(query);
            return temList;
        }


        /// <summary>
        /// 添加一个
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppInfoDto> CreateItemAsync(AppInfoAddDto input)
        {
            var _userid = base.ReadCurrentUserId();
            var newu = ObjectMapper.Map<AppInfoAddDto, AppInfo>(input);
            //添加自定义
            _dbContext.Add(newu);
            await _dbContext.SaveChangesAsync();
            var backinfo = ObjectMapper.Map<AppInfo, AppInfoDto>(newu);
            return backinfo;
        }
        /// <summary>
        /// 更新一个
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppInfoDto> UpdateItemAsync(AppInfoUpdateDto input)
        {
            var info = await _dbContext.AppInfo.Where(x => x.Id == input.Id).FirstOrDefaultAsync();
            if (info == null || info == default)
            {
                throw new UserFriendlyException("需要查询的信息不存在", "404");
            }
            ObjectMapper.Map<AppInfoUpdateDto, AppInfo>(input, info);
            await _dbContext.SaveChangesAsync();
            var backinfo = ObjectMapper.Map<AppInfo, AppInfoDto>(info);
            return backinfo;
        }
    }
}
